//
//  WeatherViewController.m
//  B-72K
//
//  Created by sos on 16/3/16.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "WeatherViewController.h"
#import "WeatherModel.h"
#import <AFNetworking.h>
#import "PMModel.h"
#import "WeatherOneColletionViewCell.h"
#import "WeatherTwoCollectionViewCell.h"
#import "LifeCollectionViewCell.h"
#import "LifeDownCollectionCell.h"
#import <UIImageView+WebCache.h>
#import "XHBrokenLine.h"
#import "WeatherWebViewController.h"

#import <CoreLocation/CoreLocation.h>
@interface WeatherViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIApplicationDelegate,CLLocationManagerDelegate>
@property (nonatomic, strong) NSMutableArray *dataArr;//所有数据
@property (nonatomic, strong) WeatherModel *toDayModel;//当天数据
@property (nonatomic, strong) WeatherModel *twoDayModel;//明天数据
@property (nonatomic, strong) WeatherModel *threeDayModel;//后天数据
@property (nonatomic, strong) UIScrollView *pageFaceView;//整页滚动视图
@property (nonatomic, strong) NSMutableArray *ThreeDayData;//今明后数据
@property (nonatomic, strong) NSMutableArray *sixData;//六天内的数据
@property (nonatomic, strong) UICollectionView *collectionView;//六天的布局
@property (nonatomic, strong) UICollectionView *collectionViewTwo;//折线下面的布局
@property (nonatomic, strong) UICollectionView *lifeCollectuinView;
@property (nonatomic, strong) NSMutableArray *lifeArr;
@property (nonatomic, strong) NSMutableArray *lifeArrM;

@property (nonatomic, strong) UICollectionView *lifeDownCollectionView;
@property (nonatomic, strong) UILabel *titles;//城市

@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic,retain) CLLocation *location;

@property (nonatomic,copy) NSString *str;


@end

@implementation WeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.str = @"北京";
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    
    //天气地位
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务当前可能尚未打开，请设置打开！");
//        self.str = @"北京";
        return;
    }
    //请求用户授权
    [_locationManager requestWhenInUseAuthorization];
    
    
    _locationManager = [[CLLocationManager alloc] init];
    // 设置代理
    _locationManager.delegate = self;
    // 设置定位精确度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置过滤器为无
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 开始定位
    [_locationManager startUpdatingLocation];

    
    
    
    //页面背景 图片
    UIImageView *blackGround = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    blackGround.image = [UIImage imageNamed:@"beijing0ne.jpg"];
    [self.view addSubview:blackGround];
    
    //整个页面的滑动视图
    self.pageFaceView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    _pageFaceView.contentSize = CGSizeMake(0, 1875*H);
    _pageFaceView.bounces = NO;
    _pageFaceView.directionalLockEnabled = YES;
    [self.view addSubview:_pageFaceView];
    
    UIView *navi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    navi.backgroundColor = [UIColor clearColor];
    [self.pageFaceView addSubview:navi];
    
    self.titles = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80*W, 40*H)];
    _titles.center = CGPointMake(navi.center.x, navi.center.y+10*H);
    _titles.font = [UIFont systemFontOfSize:20];
    _titles.backgroundColor = [UIColor clearColor];
    _titles.textAlignment = NSTextAlignmentCenter;
    _titles.text = self.str;
    [navi addSubview:_titles];
    
    //返回按钮
    UIButton *fanhuiButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    fanhuiButton.backgroundColor = [UIColor yellowColor];
    fanhuiButton.frame = CGRectMake(20*W, 20*H, 40*W, 40*H);
    [fanhuiButton addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    [fanhuiButton setTitle:@"返回" forState:UIControlStateNormal];
    [navi addSubview:fanhuiButton];
    
    //地图定位
    UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    mapButton.backgroundColor = [UIColor yellowColor];
    [mapButton setBackgroundImage:[UIImage imageNamed:@"dingwei"] forState:UIControlStateNormal];
    mapButton.frame = CGRectMake(WIDTH-50*W, 20*H, 40*W, 40*H);
    [mapButton addTarget:self action:@selector(dingwei) forControlEvents:UIControlEventTouchUpInside];
    [navi addSubview:mapButton];
    
    //获取当前时间
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYYMMddHHmm"];
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    
    NSLog(@"locationString:%@",locationString);
    
    //生活指数的图片数组
    self.lifeArr = [NSMutableArray arrayWithObjects:@"4000.png",@"3000.png",@"8000.png",@"2000.png",@"6000.png",@"1000.png",@"7000.png",@"5000.png", nil];
    
    [self data];
    
    UIView *sixdayToDay = [[UIView alloc]initWithFrame:CGRectMake(0, 682*H, WIDTH,540*H)];
    NSLog(@"!!!!!!!%lf,%lf,%lf,%lf",sixdayToDay.frame.origin.x,sixdayToDay.frame.origin.y,sixdayToDay.frame.size.width,sixdayToDay.frame.size.height);
    sixdayToDay.tag = 9999;
    sixdayToDay.backgroundColor = [UIColor blackColor];
    sixdayToDay.alpha = 0.3;
    [self.pageFaceView addSubview:sixdayToDay];
    
    //collectionview的创建
    
    //layout的创建
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    
    //设置水平滑动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 70*W)/6, 150*H);
    //    layout.minimumInteritemSpacing =5;
    layout.minimumLineSpacing = 10*W;
    layout.sectionInset = UIEdgeInsetsMake(0, 10*W, 0, 10*W);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 690*H, [UIScreen mainScreen].bounds.size.width, 150*H) collectionViewLayout:layout];
    
    //签协议
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [_collectionView registerClass:[WeatherOneColletionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor = [UIColor clearColor];
    [_pageFaceView addSubview:_collectionView];
    
    //折线下面布局
    UICollectionViewFlowLayout *layoutOne = [[UICollectionViewFlowLayout alloc]init];
    layoutOne.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layoutOne.itemSize = CGSizeMake((self.view.bounds.size.width - 70*W)/6, 80*H);
    
    layoutOne.minimumLineSpacing = 10*W;
    layoutOne.sectionInset = UIEdgeInsetsMake(0, 10*W, 0, 10*W);
    
    self.collectionViewTwo = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 950*H, WIDTH, 80*H) collectionViewLayout:layoutOne];
    self.collectionViewTwo.backgroundColor = [UIColor clearColor];
    self.collectionViewTwo.delegate = self;
    self.collectionViewTwo.dataSource = self;
    
    [self.collectionViewTwo registerClass:[WeatherTwoCollectionViewCell class] forCellWithReuseIdentifier:@"cellTwo"];
    
    [_pageFaceView addSubview:self.collectionViewTwo];
    
    //生活指数阴影
    UIView *lifeDayToDay = [[UIView alloc]initWithFrame:CGRectMake(0, 1250*H, WIDTH,460*H)];
    
    lifeDayToDay.backgroundColor = [UIColor blackColor];
    lifeDayToDay.alpha = 0.3;
    [self.pageFaceView addSubview:lifeDayToDay];
    
    //生活指数label
    UILabel *lifeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*W, 1260*H, 70*W, 20*H)];
    lifeLabel.text = @"生活指数";
    lifeLabel.textColor = [UIColor whiteColor];
    [_pageFaceView addSubview:lifeLabel];
    
    // 生活指数的collectionview
    
    UICollectionViewFlowLayout *lifeLayout = [[UICollectionViewFlowLayout alloc]init];
    
    lifeLayout.itemSize = CGSizeMake((WIDTH-20*W-1)/2, 400*H/4);
    lifeLayout.minimumInteritemSpacing = 1;
    lifeLayout.minimumLineSpacing = 1;
    lifeLayout.sectionInset = UIEdgeInsetsMake(1, 0, 0, 0);
    
    self.lifeCollectuinView = [[UICollectionView alloc]initWithFrame:CGRectMake(10*W, lifeLabel.frame.origin.y+lifeLabel.frame.size.height + 10*H, WIDTH-20, 400*H) collectionViewLayout:lifeLayout];
    
    self.lifeCollectuinView.backgroundColor = [UIColor clearColor];
    
    self.lifeCollectuinView.delegate = self;
    self.lifeCollectuinView.dataSource = self;
    
    [self.lifeCollectuinView registerClass:[LifeCollectionViewCell class] forCellWithReuseIdentifier:@"lifecell"];
    
    self.lifeCollectuinView.scrollEnabled = NO;
    
    [_pageFaceView addSubview:self.lifeCollectuinView];
    
    //生活小常识 阴影
    
    UIView *lifeDown = [[UIView alloc]initWithFrame:CGRectMake(0, 1730*H, WIDTH,460*H)];
    
    lifeDown.backgroundColor = [UIColor blackColor];
    lifeDown.alpha = 0.3;
    [self.pageFaceView addSubview:lifeDown];
    
    //生活小常识 label
    UILabel *lifeDownLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*W, 1740*H, 90*W, 20*H)];
    lifeDownLabel.text = @"生活小常识";
    lifeDownLabel.textColor = [UIColor whiteColor];
    [_pageFaceView addSubview:lifeDownLabel];
    
    //生活小常识 collectionview
    
    UICollectionViewFlowLayout *lifeDownLayout = [[UICollectionViewFlowLayout alloc]init];
    lifeDownLayout.itemSize = CGSizeMake((WIDTH-30)/2, 100*H);
    lifeDownLayout.minimumInteritemSpacing = 10;
    lifeDownLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.lifeDownCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, lifeDownLabel.frame.origin.y+lifeDownLabel.frame.size.height+10*H, WIDTH, 100*H) collectionViewLayout:lifeDownLayout];
    
    self.lifeDownCollectionView.delegate = self;
    self.lifeDownCollectionView.dataSource = self;
    
    [self.lifeDownCollectionView registerClass:[LifeDownCollectionCell class] forCellWithReuseIdentifier:@"celllife"];
    self.lifeDownCollectionView.backgroundColor = [UIColor clearColor];
    [_pageFaceView addSubview:self.lifeDownCollectionView];

    
}
-(void)dingwei
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0)
            
        {
            
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            
            //将获得的所有信息显示到label上
            
            //            NSLog(@"%@",placemark.name);
            //            UILabel *label = [self.view viewWithTag:8888];
            //            label.text = placemark.name;
            //            label.numberOfLines = 0;
            //            [label sizeToFit];
            
            //获取城市
            
            NSString *city = placemark.locality;
            NSString *ss = [city substringToIndex:[city length]-1];
            
            self.str = ss;
            NSArray *arr = self.pageFaceView.subviews;
            for (UIView *s in arr) {
                if ([[s class] isSubclassOfClass:[UICollectionView class]]) {
                    
                    continue;
                }else{
                    
                    [s removeFromSuperview];
                }
            }
            
            [self data];
            UIView *navi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
            navi.backgroundColor = [UIColor clearColor];
            [self.pageFaceView addSubview:navi];
            
            self.titles = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80*W, 40*H)];
            _titles.center = CGPointMake(navi.center.x, navi.center.y+10*H);
            _titles.font = [UIFont systemFontOfSize:20];
            _titles.backgroundColor = [UIColor clearColor];
            _titles.textAlignment = NSTextAlignmentCenter;
            _titles.text = self.str;
            [navi addSubview:_titles];
            
            //返回按钮
            UIButton *fanhuiButton = [UIButton buttonWithType:UIButtonTypeCustom];
//            fanhuiButton.backgroundColor = [UIColor yellowColor];
            fanhuiButton.frame = CGRectMake(20*W, 20*H, 40*W, 40*H);
            [fanhuiButton addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
            [fanhuiButton setTitle:@"返回" forState:UIControlStateNormal];

            [navi addSubview:fanhuiButton];
            
            //地图定位
            UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
//            mapButton.backgroundColor = [UIColor yellowColor];
            mapButton.frame = CGRectMake(WIDTH-50*W, 20*H, 40*W, 40*H);
            [mapButton addTarget:self action:@selector(dingwei) forControlEvents:UIControlEventTouchUpInside];
            [mapButton setBackgroundImage:[UIImage imageNamed:@"dingwei"] forState:UIControlStateNormal];

            [navi addSubview:mapButton];

            if (!city) {
                
                //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
                
                city = placemark.administrativeArea;
                
            }
            
            //            UILabel *label = [self.view viewWithTag:8888];
            //            label.text = city;
            
        }
        
        else if (error == nil && [placemarks count] == 0)
            
        {
            
            NSLog(@"No results were returned.");
            
        }
        
        else if (error != nil)
            
        {
            
            NSLog(@"An error occurred = %@", error);
            
        }
        
    }];
    
    //系统会一直更新数据，直到选择停止更新，因为我们只需要获得一次经纬度即可，所以获取之后就停止更新
    
    [_locationManager stopUpdatingLocation];

   
    
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    
    _location = [locations lastObject]; //取出第一个位置

    
    CLLocationCoordinate2D coordinate = _location.coordinate; //位置坐标
    NSLog(@"经度：%f,纬度：%f,海拔：%f,航向：%f,行走速度：%f",coordinate.longitude,coordinate.latitude,_location.altitude,_location.course,_location.speed);
//    UILabel *label = [self.view viewWithTag:9999];
//    label.text = [NSString stringWithFormat:@"经度：%f,纬度：%f,海拔：%f,航向：%f,行走速度：%f",coordinate.longitude,coordinate.latitude,_location.altitude,_location.course,_location.speed];
//    label.numberOfLines = 0;
//    [label sizeToFit];
    
}
-(void)data
{
//    NSString *str = [self.str ]
    NSString *urlStr = [_str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:_str]];
    NSString *url = [NSString stringWithFormat:@"http://app.tianqi.com/index.php?a=json15&area=%@&c=app&city=%@&showid=3&token=&type=1",urlStr,urlStr];
   
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.ThreeDayData = [NSMutableArray array];
        //获取今明后数据
        for (NSInteger i = 1; i<4; i++) {
            WeatherModel *m = [[WeatherModel alloc]init];
            [m setValuesForKeysWithDictionary:responseObject[@"resultvalue"][i]];
            [self.ThreeDayData addObject:m];
        }
        //所有数据
        for (NSDictionary *dic in responseObject[@"resultvalue"]) {
            
            WeatherModel *m = [[WeatherModel alloc]init];
            [m setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:m];
            //获取当天数据
            if ([m.week isEqualToString:@"今天"]) {
                self.toDayModel = m;
                
            }
            
        }
        self.sixData = [NSMutableArray array];
        //获取六天内的数据
        for (NSInteger i = 0; i< 6; i++) {
            WeatherModel *m = [[WeatherModel alloc]init];
            [m setValuesForKeysWithDictionary:responseObject[@"resultvalue"][i]];
            [self.sixData addObject:m];
            
        }
        
        //生活数据
        self.lifeArrM = [NSMutableArray array];
        NSArray *arrLife = [NSArray arrayWithObjects:@"0",@"3",@"7",@"6",@"1",@"2",@"5",@"4", nil];
        for (NSInteger i = 0; i < 8; i++) {
            if ([[self.toDayModel.lifeindex[arrLife[i]] class] isSubclassOfClass:[NSString class]]) {
                PMModel *m = [[PMModel alloc]init];
                m.name = @"暂无数据";
                m.indexStr = @"暂无数据";
                [self.lifeArrM addObject:m];
                
            }else{
                
                NSDictionary *dic = self.toDayModel.lifeindex[arrLife[i]];
                PMModel *m = [[PMModel alloc]init];
                [m setValuesForKeysWithDictionary:dic];
                [self.lifeArrM addObject:m];
            }
        }
        
        //刷新六天内天气布局数据
        [self.collectionView reloadData];
        [self.collectionViewTwo reloadData];
        [self.lifeCollectuinView reloadData];
        [self.lifeDownCollectionView reloadData];
        
        //显示视图底层
        UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, 110*W, WIDTH, 300*H)];
        downView.backgroundColor = [UIColor clearColor];
        [_pageFaceView addSubview:downView];
        
        //天气图片
        UIImageView *weatherImg = [[UIImageView alloc]initWithFrame:CGRectMake(15*W, 15*H, 50*W, 50*H)];
        [downView addSubview:weatherImg];
        
        
        //文字天气
        UILabel *weatherLabel = [[UILabel alloc]initWithFrame:CGRectMake(70*W, 25*H, 50*W, 30*H)];
        
        //        NSString *str = [self.toDayModel.weather s:1];
        weatherLabel.text = self.toDayModel.weaday;
        
        weatherLabel.font = [UIFont systemFontOfSize:20];
        weatherLabel.textColor = [UIColor whiteColor];
        //        weatherLabel.backgroundColor = [UIColor greenColor];
        [downView addSubview:weatherLabel];
        
        [WeatherViewController weatherPicker:weatherImg str:weatherLabel.text];
        
        //文字温度
        UILabel *temperaturelabel = [[UILabel alloc]initWithFrame:CGRectMake(15*W, 75*H, 200*W, 60*H)];
        temperaturelabel.text = [NSString stringWithFormat:@"%@℃",self.toDayModel.stemp];
        temperaturelabel.font = [UIFont systemFontOfSize:55];
        temperaturelabel.textColor = [UIColor whiteColor];
        //        temperaturelabel.backgroundColor = [UIColor purpleColor];
        [downView addSubview:temperaturelabel];
        
        //最低和最高温度
        UILabel *upAndDownLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*W, 160*H, 90*W, 20*H)];
        upAndDownLabel.text = [NSString stringWithFormat:@"%@/%@",self.toDayModel.ltemp,self.toDayModel.htemp];
        upAndDownLabel.textColor = [UIColor whiteColor];
        //        upAndDownLabel.backgroundColor = [UIColor orangeColor];
        [downView addSubview:upAndDownLabel];
        
        //湿度等等
        UILabel *humidityLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*W, 190*H, 170*W, 20*H)];
        humidityLabel.text = [NSString stringWithFormat:@"%@ %@ 湿度 %@",self.toDayModel.WD,self.toDayModel.WS,self.toDayModel.SD];
        humidityLabel.font = [UIFont systemFontOfSize:15];
        humidityLabel.textColor = [UIColor whiteColor];
        //        humidityLabel.backgroundColor = [UIColor yellowColor];
        [downView addSubview:humidityLabel];
        
        //预警
        UIImageView *warningView = [[UIImageView alloc]initWithFrame:CGRectMake(15*W, 225*H, 60*W, 50*H)];
        [warningView sd_setImageWithURL:[NSURL URLWithString:self.toDayModel.alarmnews.firstObject[@"img"]]];
        //        warningView.backgroundColor = [UIColor grayColor];
        [downView addSubview:warningView];
        
        //PM2.5
        UIView *pmImg = [[UIView alloc]initWithFrame:CGRectMake(300*W, 15*H, 65*W, 30*H)];
//        pmImg.backgroundColor = [UIColor redColor];
        [downView addSubview:pmImg];
        
        //pm文字状况
        UILabel *pmLabel = [[UILabel alloc]initWithFrame:CGRectMake(300*W, 45*H, 65*W, 25*H)];
        pmLabel.text = [self.toDayModel.air[@"col"] firstObject];
        
        pmLabel.textColor = [UIColor whiteColor];
        //                pmLabel.backgroundColor = [UIColor blueColor];
        pmLabel.font = [UIFont systemFontOfSize:13];
        pmLabel.textAlignment = NSTextAlignmentCenter;
        [downView addSubview:pmLabel];
        
        
        //pm数字阴影
        UIView *pmNumberView = [[UIView alloc]initWithFrame:CGRectMake(290*W, 75*H, 85*W, 25*H)];
        pmNumberView.backgroundColor = [UIColor blackColor];
        pmNumberView.alpha = 0.1;
        [downView addSubview:pmNumberView];
        
        //pm数字
        UILabel *pmNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(290*W, 75*H, 85*W, 25*H)];
        pmNumberLabel.text = [NSString stringWithFormat:@"PM2.5|%@",self.toDayModel.air[@"pm"]];
        pmNumberLabel.textColor = [UIColor whiteColor];
        pmNumberLabel.font = [UIFont systemFontOfSize:16];
        pmNumberLabel.textAlignment = NSTextAlignmentCenter;
        [downView addSubview:pmNumberLabel];
        
        //今天 明天 星期一 阴影
        UIView *dayToDay = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height -80, WIDTH, 80)];
        dayToDay.backgroundColor = [UIColor blackColor];
        dayToDay.alpha = 0.3;
        [self.pageFaceView addSubview:dayToDay];
        NSArray *arr = [NSArray arrayWithObjects:@"今天",@"明天",@"后天", nil];
        
        for (NSInteger i = 1; i<4; i++) {
            WeatherModel *m = self.ThreeDayData[i-1];
            //日期
            UILabel *labelDay = [[UILabel alloc]initWithFrame:CGRectMake(54*W*i+(i-1)*60*W, 590*H, 40*W, 20*H)];
            labelDay.font = [UIFont systemFontOfSize:15];
            labelDay.text = arr[i-1];
            labelDay.textColor = [UIColor whiteColor];
            //            labelDay.backgroundColor = [UIColor redColor];
            labelDay.textAlignment = NSTextAlignmentCenter;
            [_pageFaceView addSubview:labelDay];
            
            //天气图片
            UIImageView *tianqiImg = [[UIImageView alloc]initWithFrame:CGRectMake(40*W*i+(i-1)*60*W, 25*H, 32*W, 32*H)];
            tianqiImg.center = CGPointMake(labelDay.center.x-labelDay.frame.size.width/2,labelDay.center.y+labelDay.frame.size.height/2+16*H);
            [_pageFaceView addSubview:tianqiImg];
            
            //白天温度
            UILabel *baitianLabel = [[UILabel alloc]initWithFrame:CGRectMake(tianqiImg.frame.origin.x+tianqiImg.frame.size.width,tianqiImg.frame.origin.y, 40*W, 16*H)];
            baitianLabel.text = m.htemp;
            //            baitianLabel.backgroundColor = [UIColor blueColor];
            baitianLabel.textColor = [UIColor whiteColor];
            baitianLabel.textAlignment = NSTextAlignmentCenter;
            [_pageFaceView addSubview:baitianLabel];
            
            //晚上温度
            UILabel *nightLabel = [[UILabel alloc]initWithFrame:CGRectMake(baitianLabel.frame.origin.x, baitianLabel.frame.origin.y+baitianLabel.frame.size.height, baitianLabel.frame.size.width, baitianLabel.frame.size.height)];
            nightLabel.text = m.ltemp;
            //            nightLabel.backgroundColor = [UIColor purpleColor];
            nightLabel.textColor = [UIColor whiteColor];
            nightLabel.textAlignment = NSTextAlignmentCenter;
            [_pageFaceView addSubview:nightLabel];
            
            //类似多云～小雨这样
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(tianqiImg.frame.origin.x, tianqiImg.frame.origin.y+tianqiImg.frame.size.height, tianqiImg.frame.size.width+baitianLabel.frame.size.width, 20*H)];
            label.font = [UIFont systemFontOfSize:15];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = m.weather;
            //
            NSArray *arr = [m.weather componentsSeparatedByString:@"~"];
            NSString *str = arr.firstObject;
            //            NSString
            
            label.textColor = [UIColor whiteColor];
            [_pageFaceView addSubview:label];
            
            [WeatherViewController weatherPicker:tianqiImg str:str];
            
        }
        //折线图
        
        UIView *zhexianView = [[UIView alloc]initWithFrame:CGRectMake(0, _collectionView.frame.origin.y+_collectionView.frame.size.height-40*H, [UIScreen mainScreen].bounds.size.width, 150*H)];
        zhexianView.backgroundColor = [UIColor clearColor];
        [_pageFaceView addSubview:zhexianView];
        
        //折线数据
        NSMutableArray *zhexianArr = [NSMutableArray array];
        NSMutableArray *zhexianArrTwo = [NSMutableArray array];
        for (WeatherModel *m in self.sixData) {
            [zhexianArr addObject:m.htemp];
            [zhexianArrTwo addObject:m.ltemp];
        }
        //折线方法
        [XHBrokenLine drawTool:zhexianView dataArr:zhexianArr chartViewType:LineChartViewType color:[UIColor yellowColor]];
        
        UIView *zhexianViewTwo = [[UIView alloc]initWithFrame:CGRectMake(0, zhexianView.frame.origin.y+zhexianView.frame.size.height-100*H, WIDTH, 150*H)];
        //        zhexianViewTwo.backgroundColor = [UIColor redColor];
        [_pageFaceView addSubview:zhexianViewTwo];
        
        //折线方法
        [XHBrokenLine drawTool:zhexianViewTwo dataArr:zhexianArrTwo chartViewType:LineChartViewType color:[UIColor blueColor]];
        
        //未来天气
        UIButton *futureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        futureButton.frame = CGRectMake(60*W, 1050*H, WIDTH-120*W, 30*H);
        [futureButton setTitle:[NSString stringWithFormat:@"%@未来15天天气",self.str] forState:UIControlStateNormal];
        [futureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        futureButton.layer.borderWidth = 1;
        futureButton.layer.borderColor = [UIColor whiteColor].CGColor;
        
        [_pageFaceView addSubview:futureButton];
        
        
        //空气质量
        
        UILabel *airLabel = [[UILabel alloc]initWithFrame:CGRectMake(15*W, futureButton.frame.origin.y+futureButton.frame.size.height+20*H, 100*W, 20*H)];
        airLabel.font = [UIFont systemFontOfSize:14];
        airLabel.text = [NSString stringWithFormat:@"%@空气质量",self.toDayModel.city];
        
        airLabel.textColor = [UIColor whiteColor];
        [_pageFaceView addSubview:airLabel];
        
        UILabel *airLabelTwo = [[UILabel alloc]initWithFrame:CGRectMake(280*W, airLabel.frame.origin.y, 90*W, 20*H)];
        airLabelTwo.font = [UIFont systemFontOfSize:15];
        airLabelTwo.text = [NSString stringWithFormat:@"%@ %@",self.toDayModel.air[@"pm"],self.toDayModel.air[@"col"][0]];
        airLabelTwo.textAlignment = NSTextAlignmentCenter;
        airLabelTwo.backgroundColor = [UIColor colorWithRed:0.635 green:0.957 blue:0.102 alpha:1.000];
        
        airLabel.textColor = [UIColor whiteColor];
        [_pageFaceView addSubview:airLabelTwo];
        
        
        //颜色数组
        UIColor *colorOne = [UIColor colorWithRed:0.404 green:0.957 blue:0.349 alpha:1.000];
        UIColor *colorTwo = [UIColor colorWithRed:0.635 green:0.957 blue:0.102 alpha:1.000];
        UIColor *colorThree = [UIColor colorWithRed:0.7 green:0.412 blue:0.302 alpha:1.000];
        UIColor *colorFour = [UIColor colorWithRed:0.8 green:0.184 blue:0.169 alpha:1.000];
        UIColor *colorFive = [UIColor colorWithRed:0.9 green:0.180 blue:0.145 alpha:1.000];
        UIColor *colorSix = [UIColor colorWithRed:0.99 green:0.180 blue:0.141 alpha:1.000];
        NSArray *arrColor = [NSArray arrayWithObjects:colorOne,colorTwo,colorThree,colorFour,colorFive,colorSix, nil];
        
        //空气质量数组
        NSArray *arrNumber = [NSArray arrayWithObjects:@"50",@"100",@"150",@"200",@"300",@"500", nil];
        NSArray *arrStr = [NSArray arrayWithObjects:@"优",@"良",@"轻度",@"中度",@"重度",@"严重", nil];
        
        
        NSString *str = self.toDayModel.air[@"pm"];
        NSInteger number = str.integerValue;
        
        
        
        for (NSInteger i = 1; i < 7; i++) {
            UIView *tiaoView = [[UIView alloc]initWithFrame:CGRectMake(15*W+(i-1)*55*W+i*2, 1150*H, 56*W, 20*H)];
            tiaoView.backgroundColor = arrColor[i-1];
            [_pageFaceView addSubview:tiaoView];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tiaoView.frame.size.width, tiaoView.frame.size.height)];
            //            label.backgroundColor = [UIColor brownColor];
            label.textColor = [UIColor whiteColor];
            label.center = CGPointMake(tiaoView.center.x, tiaoView.center.y+20*H);
            label.text = arrNumber[i-1];
            label.textAlignment = NSTextAlignmentCenter;
            [_pageFaceView addSubview:label];
            
            UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, label.frame.size.width, label.frame.size.height)];
            //            labelOne.backgroundColor = [UIColor yellowColor];
            labelOne.textColor = [UIColor whiteColor];
            labelOne.center = CGPointMake(label.center.x, label.center.y+20*H);
            labelOne.text = arrStr[i-1];
            labelOne.textAlignment = NSTextAlignmentCenter;
            [_pageFaceView addSubview:labelOne];
            
            
            
            
            
        }
        NSInteger i;
        if (number >= 0 && number <= 50) {
            i = 1;
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15*W+(i-1)*55*W+i*2, 1130*H, 56*W, 20*H)];
            img.image = [UIImage imageNamed:@"sanjiao.png"];
            
            [_pageFaceView addSubview:img];
        }else if (number >50 && number <=100){
            i = 2;
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15*W+(i-1)*55*W+i*2, 1130*H, 56*W, 20*H)];
            img.image = [UIImage imageNamed:@"sanjiao.png"];
            
            [_pageFaceView addSubview:img];
        }else if (number >100 && number <=150){
            i = 3;
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15*W+(i-1)*55*W+i*2, 1130*H, 56*W, 20*H)];
            img.image = [UIImage imageNamed:@"sanjiao.png"];
            
            [_pageFaceView addSubview:img];
        }else if (number > 150 && number <= 200){
            i = 4;
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15*W+(i-1)*55*W+i*2, 1130*H, 56*W, 20*H)];
            img.image = [UIImage imageNamed:@"sanjiao.png"];
            
            [_pageFaceView addSubview:img];
        }else if (number > 200 && number <= 300){
            i = 5;
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15*W+(i-1)*55*W+i*2, 1130*H, 56*W, 20*H)];
            img.image = [UIImage imageNamed:@"sanjiao.png"];
            
            [_pageFaceView addSubview:img];
        }else{
            i = 6;
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15*W+(i-1)*55*W+i*2, 1130*H, 56*W, 20*H)];
            img.image = [UIImage imageNamed:@"sanjiao.png"];
            
            [_pageFaceView addSubview:img];
        }


        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
}

//collectionView的协议
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.lifeCollectuinView) {
        return 8;
    }else if (collectionView == self.lifeDownCollectionView){
        
        return 2;
    }else
    {
        return 6;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    weatherModel *m = self.sixData[indexPath.row];
    if (collectionView == self.collectionView) {
        
        WeatherOneColletionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        WeatherModel *m = self.sixData[indexPath.row];
        cell.labelOne.text = m.week;//星期
        cell.labelTwo.text = m.date;//日期
        cell.labelThree.text = m.weaday;// 文字天气
        cell.labelFour.text = m.htemp;
        [WeatherViewController weatherPicker:cell.img str:m.weaday];
        //        cell.backgroundColor = [UIColor greenColor];
        
        return cell;
    }else if(collectionView == self.collectionViewTwo){
        WeatherModel *m = self.sixData[indexPath.row];
        
        WeatherTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellTwo" forIndexPath:indexPath];
        cell.labelOne.text = m.ltemp;
        cell.labelTwo.text = m.weanight;
        
        [WeatherViewController weatherPicker:cell.img str:m.weanight];
        
        return cell;
    }else if (collectionView == self.lifeCollectuinView){
        LifeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lifecell" forIndexPath:indexPath];
        //        if (indexPath.row % 2 == 0) {
        //            cell.lineLabelOne.backgroundColor = [UIColor clearColor];
        //        }
        
        
        cell.img.image = [UIImage imageNamed:self.lifeArr[indexPath.row]];
        cell.labelOne.text = [self.lifeArrM[indexPath.row] name];
        cell.labelTwo.text = [self.lifeArrM[indexPath.row] indexStr];
        return cell;
        
        
    }else{
        LifeDownCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"celllife" forIndexPath:indexPath];
        NSArray *arr = self.toDayModel.news;
        [cell.img sd_setImageWithURL:[NSURL URLWithString:arr[indexPath.row][@"img"]]];
        cell.label.text = arr[indexPath.row][@"title"];
        
        
        return cell;
        
    }
    
    
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == self.lifeDownCollectionView) {
        NSArray *arr = self.toDayModel.news;
        WeatherWebViewController *web = [[WeatherWebViewController alloc]init];
        web.url = arr[indexPath.row][@"url"];
        [self.navigationController pushViewController:web animated:YES];
        
        
    }
    
    
}
//天气图片
+(UIView *)weatherPicker:(UIView *)view str:(NSString *)str
{
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    [view addSubview:img];
    if ([str isEqualToString:@"多云"]) {
        img.image = [UIImage imageNamed:@"duoyun.png"];
    }else if ([str isEqualToString:@"阴"]){
        img.image = [UIImage imageNamed:@"yin.png"];
    }else if ([str isEqualToString:@"小雨"]){
        img.image = [UIImage imageNamed:@"xiaoyu.png"];
        
    }else if ([str isEqualToString:@"晴"]){
        img.image = [UIImage imageNamed:@"qing.png"];
        
    }else if ([str isEqualToString:@"阵雨"]){
        img.image = [UIImage imageNamed:@"zhenyu.png"];
    }
    
    
    
    
    
    return view;
}
-(void)fanhui
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
